package com.abner.design.patterns.mediator;

import java.util.HashMap;

/**
 * 中介者具体实现
 * @author abner
 */
public class ConcreteMediator extends Mediator {


	private HashMap<String, Colleague> colleagueMap;

	private HashMap<String, String> interMap;

	public ConcreteMediator() {
		colleagueMap = new HashMap<String, Colleague>();
		interMap = new HashMap<String, String>();
	}

	/**
	 * 将需要中介管理的东西注册进去
	 * @param colleagueName
	 * @param colleague
	 */
	@Override
	public void register(String colleagueName, Colleague colleague) {

		colleagueMap.put(colleagueName, colleague);

		if (colleague instanceof Alarm) {
			interMap.put("Alarm", colleagueName);
		} else if (colleague instanceof CoffeeMachine) {
			interMap.put("CoffeeMachine", colleagueName);
		} else if (colleague instanceof TV) {
			interMap.put("TV", colleagueName);
		} else if (colleague instanceof Curtains) {
			interMap.put("Curtains", colleagueName);
		}

	}

	/**
	 * 根据消息，完成对应任务
	 * 中介者协调各个具体的对象,完成任务
	 * @param stateChange
	 * @param colleagueName
	 */
	@Override
	public void getMessage(int stateChange, String colleagueName) {

		if (colleagueMap.get(colleagueName) instanceof Alarm) {
			if (stateChange == 0) {
				((CoffeeMachine) (colleagueMap.get(interMap
						.get("CoffeeMachine")))).StartCoffee();
				((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();
			} else if (stateChange == 1) {
				((TV) (colleagueMap.get(interMap.get("TV")))).StopTv();
			}

		} else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
			((Curtains) (colleagueMap.get(interMap.get("Curtains"))))
					.UpCurtains();

		} else if (colleagueMap.get(colleagueName) instanceof TV) {

		} else if (colleagueMap.get(colleagueName) instanceof Curtains) {

		}

	}

	/**
	 * 发送消息
	 */
	@Override
	public void sendMessage() {

	}

}
